package com.cskaoyan.mall.controller;

import com.cskaoyan.mall.model.Result;
import com.cskaoyan.mall.model.bo.PageOrdersBO;
import com.cskaoyan.mall.model.bo.UpdateOrderBO;
import com.cskaoyan.mall.model.vo.GetOrderVO;
import com.cskaoyan.mall.service.OrdersService;
import com.cskaoyan.mall.service.OrdersServiceImpl;
import com.cskaoyan.mall.utils.HttpUtils;
import com.cskaoyan.mall.utils.StringUtils;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Map;

@WebServlet("/api/admin/order/*")
public class OrdersServlet extends HttpServlet {
    private Gson gson = new Gson();
    private OrdersService ordersService = new OrdersServiceImpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String action = requestURI.replace("/api/admin/order/", "");
        if ("ordersByPage".equals(action)) {
            ordersByPage(request,response);
        }else if ("changeOrder".equals(action)) {
            changeOrder(request,response);
        }
    }

    /**
     * 更新订单详情
     * @param request
     * @param response
     */
    private void changeOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        UpdateOrderBO updateOrderBO = null;
        try {
            updateOrderBO = gson.fromJson(requestBody,UpdateOrderBO.class);
            if (!StringUtils.isNumeric(updateOrderBO.getId())) {
                response.getWriter().println(gson.toJson(Result.error("输入的参数不合法")));
                return;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        int code = ordersService.changeOrder(updateOrderBO);
        if (code == 200) {
            response.getWriter().println(gson.toJson(Result.ok()));
            return;
        }
        response.getWriter().println(gson.toJson(Result.error("修改订单失败")));
    }

    /**
     * 分页查询
     * @param request
     * @param response
     * address: ""
     * currentPage: 1
     * goods: ""
     * id: ""
     * moneyLimit1: ""
     * moneyLimit2: ""
     * name: ""
     * pagesize: 5
     * state: -1
     */
    private void ordersByPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        PageOrdersBO pageOrdersBO = null;
        try {
            //构造请求
            pageOrdersBO = gson.fromJson(requestBody,PageOrdersBO.class);
            //金额的上限和下限可以不传，但是如果传的话那么一定是数字
            if (!StringUtils.isEmpty(pageOrdersBO.getMoneyLimit1())) {
                Double.parseDouble(pageOrdersBO.getMoneyLimit1());
            }
            if (!StringUtils.isEmpty(pageOrdersBO.getMoneyLimit2())) {
                Double.parseDouble(pageOrdersBO.getMoneyLimit2());
            }
        } catch (Exception e) {
            response.getWriter().println(gson.toJson(Result.error("参数类型不匹配")));
            return;
        }
        if (!StringUtils.isEmpty(pageOrdersBO.getId())) {
            if (!StringUtils.isNumeric(pageOrdersBO.getId())) {
                response.getWriter().println(gson.toJson(Result.error("参数输入不合法")));
                return;
            }
        }
        //校验 全部的参数
        System.out.println(pageOrdersBO);
        Map<String,Object> result = ordersService.ordersByPage(pageOrdersBO);
        response.getWriter().println(gson.toJson(Result.ok(result)));
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String action = requestURI.replace("/api/admin/order/", "");
        if ("order".equals(action)) {
            order(request,response);
        }else if ("deleteOrder".equals(action)) {
            deleteOrder(request,response);
        }
    }

    /**
     * 删除订单
     * @param request
     * @param response
     */
    private void deleteOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        int code = ordersService.deleteOrder(id);
        if (code == 200) {
            response.getWriter().println(gson.toJson(Result.ok()));
            return;
        }
        response.getWriter().println(gson.toJson(Result.error("删除订单失败")));
    }

    /**
     * {"code":0,
     * "data":{"id":2076,"amount":2.0,"num":1,"goodsDetailId":1869,"state":1,"goods":"帽子",
     * "spec":[{"id":1241,"specName":"x","unitPrice":0.0},
     * {"id":1242,"specName":"l","unitPrice":0.0},
     * {"id":1517,"specName":"xl","unitPrice":0.0},
     * {"id":1869,"specName":"s","unitPrice":0.0},
     * {"id":1870,"specName":"xs","unitPrice":0.0}],
     * "states":[{"id":0,"name":"未付款"},{"id":1,"name":"未发货"},{"id":2,"name":"已发货"},{"id":3,"name":"已完成订单"}],
     * "curState":{"id":1},
     * "curSpec":{"id":1869}}}
     *
     * 传来的是id
     * 返回当前的订单详情
     * @param request
     * @param response
     */
    private void order(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        GetOrderVO getOrderVO= ordersService.order(id);
        System.out.println(getOrderVO);
        response.getWriter().println(gson.toJson(Result.ok(getOrderVO)));
    }
}
